package com.example.demo.laws.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.CommonUtil;
import com.example.demo.laws.pojo.AeolLaws;
import com.example.demo.laws.service.impl.AeolLawsServiceImpl;

/**
 * 
 * <p>
 * 功能描述：法律法规 控制层
 * </p>
 * 
 * @author lixh
 * @date 2019年1月11日 下午4:02:22
 * @company chinobot
 * @version 1.0
 */
@RestController
@RequestMapping("/base/laws")
public class AeolLawsController {
	
	@Autowired
	AeolLawsServiceImpl aeollawsserviceimpl;

	@RequestMapping(value = "/showlist", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String showlist(HttpServletRequest request, HttpServletResponse response) {
		// RequestMappingHandlerMapping
		// DispatcherServlet
		JSONObject object = new JSONObject();
		Map map = new HashMap<String, Object>();
		String lawNames = request.getParameter("lawNames");
		String status = request.getParameter("status");
		String pageNumstr = request.getParameter("pagenum");
		String pageSizestr = request.getParameter("pagesize");
		int pageNum = 0;
		int pageSize = 0;
		if (!StringUtils.isEmpty(pageNumstr)) {
			pageNum = Integer.valueOf(pageNumstr);
		}
		if (!StringUtils.isEmpty(pageSizestr)) {
			pageSize = Integer.valueOf(pageSizestr);
		}
		if (pageNum == 0 || pageSize == 0) {
			pageNum = 1;
			pageSize = 10;
		}
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		if (!StringUtils.isEmpty(lawNames)) {
			map.put("lawName", lawNames);
		}
		if (!StringUtils.isEmpty(status)) {
			map.put("status", status);
		}
		List<Map<String, Object>> map2 = aeollawsserviceimpl.getLawsList(map);
		for (int i = 0; i < map2.size(); i++) {
			Map<String, Object> map3 = map2.get(i);
			String issueTimes = String.valueOf(map3.get("ISSUE_TIME"));
			if (!StringUtils.isEmpty(issueTimes) && !issueTimes.equals("null")) {
				if (CommonUtil.isNumeric(issueTimes)) {
					map3.put("ISSUE_TIME", new Date(new Long(issueTimes)));
				} else {
					map3.put("ISSUE_TIME", issueTimes.toString());
				}
			} else {
				map3.put("ISSUE_TIME", "");
			}
			// map3.put("ISSUE_TIME",
			// CommonUtil.timeStamp2Date(String.valueOf(map3.get("ISSUE_TIME")),
			// "yyyy-MM-dd"));
			// map3.put("INPUT_TIME",
			// CommonUtil.timeStamp2Date(String.valueOf(map3.get("INPUT_TIME")),
			// "yyyy-MM-dd"));
			String inputTimes = String.valueOf(map3.get("INPUT_TIME"));
			if (!StringUtils.isEmpty(inputTimes) && !inputTimes.equals("null")) {
				if (CommonUtil.isNumeric(inputTimes)) {
					map3.put("INPUT_TIME", new Date(new Long(inputTimes)));
				} else {
					map3.put("INPUT_TIME", inputTimes.toString());
				}
			} else {
				map3.put("INPUT_TIME", "");
			}
			map3.put("STATUS", CommonUtil.ifStatus(String.valueOf(map3.get("STATUS"))));
			map3.put("LAW_TYPE", CommonUtil.LawsType(String.valueOf(map3.get("LAW_TYPE"))));
		}
		String jsonString = object.toJSONString(map2);
		return jsonString;
	}

	/**
	 * 
	 * 方法描述: 保存方法
	 * 
	 * @return
	 * @author lixh
	 * @throws Exception
	 */
	@RequestMapping(value = "/save", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject object = new JSONObject();
		String lawsNos = request.getParameter("lawsNo");
		String lawName = request.getParameter("lawName");
		String status = request.getParameter("status");
		String inputPerson = request.getParameter("inputPerson");
		String lawGroup = request.getParameter("lawGroup");
		String lawDetail = request.getParameter("lawDetail");
		String lawType = request.getParameter("lawType");
		String issueTime = request.getParameter("issueTime");
		String inputTime = request.getParameter("inputTime");
		AeolLaws aeollaws = new AeolLaws();
		Long lawsNo = aeollawsserviceimpl.getLawsNo();
		if (StringUtils.isEmpty(lawsNo)) {
			lawsNo = 4403000000000000L;
		}
		if (!StringUtils.isEmpty(lawName) && !lawName.equals("undefined")) {
			if (lawName.trim().length() > 50) {
				object.put("code", "404");
				object.put("message", "保存失败,法律法规名称长度不能超过50!");
				return object.toJSONString();
			} else {
				aeollaws.setLawName(lawName);
			}
		} else {
			object.put("code", "404");
			object.put("message", "保存失败,法律法规名称不能为空!");
			return object.toJSONString();
		}
		if (!StringUtils.isEmpty(status) && !status.equals("undefined")) {
			aeollaws.setStatus(status);
		}
		if (!StringUtils.isEmpty(inputPerson) && !inputPerson.equals("undefined")) {
			if (inputPerson.length() > 25) {
				object.put("code", "404");
				object.put("message", "保存失败,录入人长度不能超过25!");
				return object.toJSONString();
			} else {
				aeollaws.setInputPerson(inputPerson);
			}
		} else {
			object.put("code", "404");
			object.put("message", "保存失败,录入人不能为空");
			return object.toJSONString();
		}
		if (!StringUtils.isEmpty(lawGroup) && !lawGroup.equals("undefined")) {
			aeollaws.setLawGroup(lawGroup);
		} else {
			object.put("code", "404");
			object.put("message", "保存失败,所属部门不能为空");
			return object.toJSONString();
		}
		if (!StringUtils.isEmpty(lawDetail) && !lawDetail.equals("undefined")) {
			if (lawDetail.trim().length() > 998) {
				object.put("code", "404");
				object.put("message", "保存失败,法律条款明细长度不能超过998!");
				return object.toJSONString();
			} else {
				aeollaws.setLawDetail(lawDetail);
			}
		} else {
			object.put("code", "404");
			object.put("message", "保存失败,法律条款明细不能为空");
			return object.toJSONString();
		}
		if (!StringUtils.isEmpty(lawType) && !lawType.equals("undefined")) {
			aeollaws.setLawType(lawType);
		} else {
			object.put("code", "404");
			object.put("message", "保存失败,法律类别不能为空");
			return object.toJSONString();
		}
		if (!StringUtils.isEmpty(issueTime) && !issueTime.equals("undefined")) {
			if (CommonUtil.isNumeric(issueTime)) {
				aeollaws.setIssueTime(new Date(new Long(issueTime)));
			} else {
				aeollaws.setIssueTime(CommonUtil.formatDate(issueTime));
			}
		} else {
			object.put("code", "404");
			object.put("message", "保存失败");
			return object.toJSONString();
		}
		if (!StringUtils.isEmpty(inputTime) && !inputTime.equals("undefined")) {
			if (CommonUtil.isNumeric(inputTime)) {
				aeollaws.setInputTime(new Date(new Long(inputTime)));
			} else {
				aeollaws.setInputTime(CommonUtil.formatDate(inputTime));
			}
		} else {
			object.put("code", "404");
			object.put("message", "保存失败");
			return object.toJSONString();
		}
		// 如果从request中获取到 主键id 就判断是更新
		if (!StringUtils.isEmpty(lawsNos)) {
			aeollaws.setLawNo(Long.valueOf(lawsNos));
			int results = aeollawsserviceimpl.update(aeollaws);
			if (results > 0) {
				object.put("code", "200");
				object.put("message", "修改成功");
				return object.toJSONString();
			}
		}
		aeollaws.setLawNo(lawsNo + 1L);
		String reslut = aeollawsserviceimpl.add(aeollaws);
		if (!StringUtils.isEmpty(Integer.valueOf(reslut)) && Integer.valueOf(reslut) > 0) {
			object.put("code", "200");
			object.put("message", "保存成功");
		} else {
			object.put("code", "404");
			object.put("message", "保存失败");
		}
		return object.toJSONString();
	}

	/**
	 * 
	 * 方法描述:删除一条记录
	 * 
	 * @return
	 * @author lixh
	 */
	@RequestMapping(value = "/delete", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject object = new JSONObject();
		String lawsNo = request.getParameter("lawsNo");
		if (StringUtils.isEmpty(lawsNo)) {
			object.put("code", "404");
			object.put("message", "系统异常");
		}
		int result = aeollawsserviceimpl.delete(lawsNo);
		if (result >= 0) {
			object.put("code", "200");
			object.put("message", "删除成功");
		}
		return object.toJSONString();
	}

	/**
	 * 
	 * 方法描述: 查看功能
	 * 
	 * @return
	 * @throws Exception
	 * @author lixh
	 */
	@RequestMapping(value = "/look", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String look(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject object = new JSONObject();
		String lawsNo = request.getParameter("lawsNo");
		if (StringUtils.isEmpty(lawsNo)) {
			object.put("code", "404");
			object.put("message", "主键为空，无法查询");
			return object.toJSONString();
		}
		AeolLaws aeollaws = aeollawsserviceimpl.getAeolLaws(lawsNo);
		if (StringUtils.isEmpty(aeollaws)) {
			object.put("code", "404");
			object.put("message", "查询不到信息");
			return object.toJSONString();
		}
		object.put("lawName", aeollaws.getLawName());
		object.put("status", aeollaws.getStatus());
		object.put("issueTime", aeollaws.getIssueTime());
		object.put("inputTime", aeollaws.getInputTime());
		object.put("inputPerson", aeollaws.getInputPerson());
		object.put("lawType", aeollaws.getLawType());
		object.put("lawGroup", aeollaws.getLawGroup());
		object.put("lawDetail", aeollaws.getLawDetail());
		object.put("code", "200");
		return object.toJSONString();
	}

}
